<template>
  <div class="reqVisit">
    <h1>来访登记</h1>
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="100px"
    >
      <el-form-item label="访问门牌" prop="visitRoom">
        <el-input
          v-model="dataForm.visitRoom"
          placeholder="访问门牌"
        ></el-input>
      </el-form-item>
      <el-form-item label="访问时间" prop="combineTime">
        <el-date-picker
          v-model="dataForm.combineTime"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="访客姓名" prop="visitorName">
        <el-input v-model="dataForm.visitorName" placeholder="访客姓名"></el-input>
      </el-form-item>
      <el-form-item label="身份证号" prop="visitorIdCard">
        <el-input
          v-model="dataForm.visitorIdCard"
          placeholder="身份证号"
        ></el-input>
      </el-form-item>
      <el-form-item label="手机号码" prop="visitorTelephone">
        <el-input
          v-model="dataForm.visitorTelephone"
          placeholder="手机号码"
          maxlength="11"
        ></el-input>
      </el-form-item>
      <el-form-item label="访问事由" prop="visitReason">
        <el-radio-group v-model="dataForm.visitReason">
          <el-radio label="洽公" name="visitReason"></el-radio>
          <el-radio label="探亲" name="visitReason"></el-radio>
          <el-radio label="外卖" name="visitReason"></el-radio>
          <el-radio label="快递" name="visitReason"></el-radio>
          <el-radio label="其他" name="visitReason"></el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <el-button class="mid" type="primary" @click="dataFormSubmit()"
      >确定</el-button
    >
  </div>
</template>

<script>
import { formatTimer } from '@utils'

export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: 0,
        visitRoom: '',
        visitTime: '',
        visitorName: '',
        visitorIdCard: '',
        visitorTelephone: '',
        visitReason: '洽公',
        leaveTime: ''
      },
      dataRule: {
        visitRoom: [
          { required: true, message: '访问地点不能为空', trigger: 'blur' }
        ],
        combineTime: [
          { required: true, message: '访问时间不能为空', trigger: 'blur' }
        ],
        visitorName: [
          { required: true, message: '姓名不能为空', trigger: 'blur' }
        ],
        visitorIdCard: [
          { required: true, message: '身份证号不能为空', trigger: 'blur' }
        ],
        visitorTelephone: [
          { required: true, message: '手机号码不能为空', trigger: 'blur' }
        ],
        visitReason: [
          { required: true, message: '访问事由不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init (id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/covid/visitor/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.visitRoom = data.visitor.visitRoom
              this.dataForm.visitTime = data.visitor.visitTime
              this.dataForm.visitorName = data.visitor.visitorName
              this.dataForm.visitorIdCard = data.visitor.visitorIdCard
              this.dataForm.visitorTelephone = data.visitor.visitorTelephone
              this.dataForm.visitReason = data.visitor.visitReason
              this.dataForm.leaveTime = data.visitor.leaveTime
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.dataForm.visitTime = formatTimer(this.dataForm.combineTime[0])
      this.dataForm.leaveTime = formatTimer(this.dataForm.combineTime[1])

      console.log(this.dataForm)
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/covid/visitor/${!this.dataForm.id ? 'save' : 'update'}`
            ),
            method: 'post',
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              visitRoom: this.dataForm.visitRoom,
              visitTime: this.dataForm.visitTime,
              visitorName: this.dataForm.visitorName,
              visitorIdCard: this.dataForm.visitorIdCard,
              visitorTelephone: this.dataForm.visitorTelephone,
              visitReason: this.dataForm.visitReason,
              leaveTime: this.dataForm.leaveTime
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
.reqVisit {
  display: flex;
  flex-direction: column;
  margin: 0 auto;
  width: 70%;
  justify-content: center;
  h1,
  .mid {
    margin: 20px auto;
  }
}
</style>
